package script.mainpage
{
	import eventUtil.EventCenter;
	
	import laya.components.Script;
	import laya.events.Event;
	import laya.maths.Point;
	import laya.maths.Rectangle;
	import laya.utils.Handler;
	
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.goods.GoodsItemVo;
	
	import script.mainpage.items.HomeGoodsCell;
	
	import ui.tabview.HomePanelUI;
	
	
	public class HomeController extends Script
	{
		private var uiSkin:HomePanelUI;
		private var downList:Boolean = false;
		
		private var downPosY:Number = 0;
		private var starty:Number = 0;
		public function HomeController()
		{
			super();
		}
		
		override public function onStart():void
		{
			uiSkin = this.owner as HomePanelUI;
			
			uiSkin.goodsList.itemRender = HomeGoodsCell;
			
			uiSkin.goodsList.vScrollBarSkin = "";
			uiSkin.goodsList.repeatX = 2;
			uiSkin.goodsList.spaceY = 20;
			uiSkin.goodsList.spaceX = 35;
			uiSkin.goodsList.renderHandler = new Handler(this, updateGoodsList);
			uiSkin.goodsList.selectHandler = new Handler(this,selectGoods);
			uiSkin.goodsList.selectEnable = true;
			uiSkin.goodsList.array = [];
			
			uiSkin.goodsList.on(Event.MOUSE_DOWN,this,onCheckDrag);
			uiSkin.goodsList.on(Event.CLICK,this,onCheckDrag);
			uiSkin.goodsList.on(Event.MOUSE_UP,this,onStopDrag);
			uiSkin.goodsList.on(Event.MOUSE_OUT,this,onStopDrag);

			starty = uiSkin.goodsList.y;
			var btnlist:Array = [uiSkin.vegetalbes,uiSkin.meat,uiSkin.fruits,uiSkin.fish];
			
			for(var i=0;i < btnlist.length;i++)
			{
				btnlist[i].on(Event.CLICK,this,onSeleteCategory,[i]);
			}
			uiSkin.searchBtn.on(Event.CLICK,this,refreshGoodsList);
			//var postdata:String = "type=" + param;
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getGoodsList,this,onGeGoodsBack,null,"post");
			
		}
		
		private function onSeleteCategory(index:int):void
		{
			EventCenter.instance.event(EventCenter.SHOW_CONTENT_PANEL,[1,index]);

		}
		
		private function onCheckDrag(e:Event):void
		{
			downList = true;
			downPosY = e.stageY;
			if(uiSkin.goodsList.scrollBar.value <= 0)
			{

				uiSkin.goodsList.on(Event.MOUSE_MOVE,this,onMoveGoodlist);
			}
		}
		
		private function onMoveGoodlist(e:Event):void
		{
			var distance:Number = e.stageY - downPosY;
			if(distance > 0 && distance < 100)
			{
				uiSkin.goodsList.y = starty + distance;
			}
			else if(distance >= 100)
			{
				uiSkin.goodsList.y = starty + 100;
				
			}
		}
		private function onStopDrag():void
		{
			uiSkin.goodsList.off(Event.MOUSE_MOVE,this,onMoveGoodlist);
			if(uiSkin.goodsList.y >= starty + 100)
			{
				uiSkin.goodsList.y = starty;
				refreshGoodsList();
			}else if(uiSkin.goodsList.y > starty )
			{
				uiSkin.goodsList.y = starty;
				
			}
		}
		private function onGeGoodsBack(data:Object):void
		{
			if(uiSkin.destroyed)
				return;
			var result:Object = JSON.parse(data as String);
			
			if(result.code == HttpErrorCode.OK)
			{
				var goodslist:Array = [];
				for(var i:int=0;i < result.data.length;i++)
				{
					goodslist.push(new GoodsItemVo(result.data[i]));
				}
				
				uiSkin.goodsList.array = goodslist;
			}
			
		}
		
		private function refreshGoodsList():void
		{
			var postdata:String = "goodsName=" + uiSkin.searchInput.text;

			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getGoodsList,this,onGeGoodsBack,postdata,"post");

		}
		
		private function updateGoodsList(cell:HomeGoodsCell):void
		{
			cell.setData(cell.dataSource);
		}
		
		private function selectGoods(index:int):void
		{
			
		}
		
		public override function onDestroy():void
		{
			console.log("destroy hoe");
		}
		
	}
}